package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import android.os.Bundle;

import com.cj.mobile.iabp.app.config.CommonRetryWithDelay;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.app.utils.RxUtils;
import com.cj.mobile.iabp.mvp.contract.HomePatientContract;
import com.cj.mobile.iabp.mvp.model.entity.DeptInfo;
import com.cj.mobile.iabp.mvp.model.entity.PatientInfo;
import com.cj.mobile.iabp.mvp.ui.adapter.PatientInfoAdapter;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/02 22:49
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HomePatientPresenter extends BasePresenter<HomePatientContract.Model, HomePatientContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public HomePatientPresenter(HomePatientContract.Model model, HomePatientContract.View rootView) {
        super(model, rootView);
    }

    // 数据源
    @Inject
    List<PatientInfo> mList;

    // 内容适配器
    @Inject
    PatientInfoAdapter mAdapter;

    // 关键字
    private String key;
    // 院区
    private String deptSec;
    // 科室
    private String deptId;
    // 状态
    private String state;

    public void initData(Bundle bundle) {
        // 获取默认值
        if (bundle != null) {
            deptSec = bundle.getString("deptSec", "");
            state = bundle.getString("state", "");
        }

        mRootView.showInitData(deptSec,state);

        getDepartmentList(true, deptSec);
    }

    /**
     * 获取科室列表
     */
    public void getDepartmentList(boolean init, String parentId) {
        if (IS_DEBUG_DATA) {
            List<DeptInfo> mList = new ArrayList<>();
            if (parentId.equals("2")) {
                // 朝阳院区
                mList.add(new DeptInfo(4, 2, "主动脉外科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(5, 2, "主动脉外科中心四区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(6, 2, "冠心病外科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(7, 2, "冠心病外科中心四区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(8, 2, "小儿心脏科中心二区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(9, 2, "心真与膜外科中心", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));

            } else if (parentId.equals("3")) {
                // 通州院区
                mList.add(new DeptInfo(14, 3, "心外监护室三区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(15, 3, "心外监护室一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(16, 3, "微创外科中心一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(17, 3, "结构性心脏病中心一区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(18, 3, "瓣膜与房颜外科中心", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
                mList.add(new DeptInfo(19, 3, "小儿心脏科中心-区", "1", "2025-03-19 11:11:42", "admin", "2025-03-19 17:34:11", "admin", "5", "0"));
            }

            mRootView.loadDepartment(mList);

            getPatientInfo(true);
        } else {
            mModel.getDeptList(parentId)
                    .compose(RxUtils.applySchedulers(mRootView, false, init))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<List<DeptInfo>>(mErrorHandler) {
                        @Override
                        public void onError(Throwable t) {
                            super.onError(t);

                            getPatientInfo(true);
                        }

                        @Override
                        public void onNext(List<DeptInfo> info) {
                            mRootView.loadDepartment(info);
                            getPatientInfo(true);
                        }
                    });

        }
    }

    /**
     * 获取患者信息
     */
    public void getPatientInfo(boolean init) {

        if (IS_DEBUG_DATA) {
            //清空列表
            mList.clear();

            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "90",
                    "2739702", "1", "王越", "结构", "62", "1", "160", "75", "29", "结构性心脏病中心二区",
                    "2", "朝阳院区", "", "", "冠心病", "0", "1", "i3", "20", "", "", "33c9591de1cfba80dfc98facac0c265f", "", ""));
            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "84",
                    "52904826", "1", "王美芹", "冠二", "68", "2", "155", "67", "25", "冠心病外科中心二区",
                    "2", "朝阳院区", "右冠起源异常", "", "冠心病", "2", "1", "I5", "16", "", "", "ebeefa95219c9176bd91ec4a8f2681fe", "", ""));
            mList.add(new PatientInfo("", "2025-08-22 08:11:46", "", "", "", "0", "", "72",
                    "60885579", "1", "王润月", "冠二", "70", "1", "162", "75", "25", "冠心病外科中心二区",
                    "2", "朝阳院区", "", "", "冠心病", "2", "1", "i5", "26", "", "", "e4c6f074d06709d80da0da55b9092cd1", "", ""));

            mAdapter.notifyDataSetChanged();
        } else {
            mModel.getPatientInfo(key, deptSec, deptId, state)
                    .compose(RxUtils.applySchedulers(mRootView, init, false))
                    .subscribe(new ErrorHandleSubscriber<List<PatientInfo>>(mErrorHandler) {

                        @Override
                        public void onNext(List<PatientInfo> infoResponse) {
                            if (infoResponse != null) {
                                mList.clear();

                                if (CommonUtils.isNotEmpty(infoResponse))
                                    mList.addAll(infoResponse);

                                // 显示“暂无信息”
                                mRootView.isNotInfo(mList.isEmpty());

                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    });

        }
    }


    /**
     * 删除患者
     */
    public void removePatient(String id) {
        if (IS_DEBUG_DATA) {
            getPatientInfo(false);
        } else {
            mModel.removePatient(id, "1")
                    .subscribeOn(Schedulers.io())
                    .retryWhen(new CommonRetryWithDelay(2, 2))    // 遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoadingSubmit();
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {
                        mRootView.hideLoadingSubmit();
                    }).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {

                        @Override
                        public void onNext(String commonResponse) {
                            mRootView.showMessage("删除患者成功！");
                            getPatientInfo(false);
                        }
                    });
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDeptSec() {
        return deptSec;
    }

    public void setDeptSec(String deptSec) {
        this.deptSec = deptSec;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        if (CommonUtils.isNotEmpty(mList)) {
            mList.clear();
            mList = null;
        }
        this.mAdapter = null;
    }
}